package com.example.demo.filter;

import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Order(1) //过滤器的顺序
@WebFilter(urlPatterns = "/*", filterName = "ParamsFilter")
//urlPatterns = "/*" 表示为所有请求实现过滤，filterName = "ParamsFilter" 表示为过滤器起名为“ParamsFilter”
@Component
public class ParamsFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //filter对象只会创建一次，init方法也只会执行一次。
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        ParamsWrapper requestWrapper = new ParamsWrapper((HttpServletRequest) request);
        filterChain.doFilter(requestWrapper, response);
        response.setCharacterEncoding("UTF-8");
    }

    @Override
    public void destroy() {

    }
}

